package com.clx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clx.constants.SystemConstants;
import com.clx.domain.ResponseResult;
import com.clx.domain.entity.Article;
import com.clx.domain.vo.ArticleDetailVo;
import com.clx.domain.vo.ArticleListVo;
import com.clx.domain.vo.HotArticleVo;
import com.clx.domain.vo.PageVo;
import com.clx.mapper.ArticleMapper;
import com.clx.service.ArticleService;
import com.clx.service.CategoryService;
import com.clx.utils.BeanCopyUtils;
import com.clx.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisCache redisCache;

    @Override
    public ResponseResult hotArticleList() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.orderByDesc(Article::getViewCount);

        Page<Article> page = new Page<>(SystemConstants.DEFAULT_ARTICLE_CURRENT_PAGE, SystemConstants.DEFAULT_ARTICLE_PAGE_SIZE);
        page(page, queryWrapper);
        List<Article> articles = page.getRecords();
        List<HotArticleVo> articleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);
        return ResponseResult.okResult(articleVos);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper();
        // 分页查询
        // 查询条件
        // category正确&&状态是正式发布
        queryWrapper.eq(Objects.nonNull(categoryId) && categoryId > 0, Article::getCategoryId, categoryId);
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);

        // 对isTop进行降序
        queryWrapper.orderByDesc(Article::getIsTop);


        Page<Article> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        List<Article> articles = page.getRecords();

//        for (Article article : articles) {
//            article.setCategoryName(categoryService.getById(article.getCategoryId()).getName());
//        }

        articles.stream()
                .map(article -> article.setCategoryName(categoryService.getById(article.getCategoryId()).getName()))
                .collect(Collectors.toList());

        // 封装查询结果
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(articles, ArticleListVo.class);
          PageVo pageVo = new PageVo(articleListVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult articleDetail(Long id) {
        Article article = getById(id);
        // 从Redis中获取viewCount
        Integer viewCount = redisCache.getCacheMapValue(SystemConstants.REDIS_BLOG_ARTICLE_VIEW_COUNT, article.getId().toString());
        article.setViewCount(viewCount.longValue());

        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        articleDetailVo.setCategoryName(categoryService.getById(article.getCategoryId()).getName());
        return ResponseResult.okResult(articleDetailVo);
    }

    @Override
    public ResponseResult updateViewCount(Long id) {
        // 更新redis中对应id的浏览量
        redisCache.incrementCacheMapValue(SystemConstants.REDIS_BLOG_ARTICLE_VIEW_COUNT, id.toString(), SystemConstants.REDIS_BLOG_ARTICLE_VIEW_COUNT_ADD_UNIT);
        return ResponseResult.okResult();
    }
}
